<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class Speaker extends Model
{
    use HasDateTimeFormatter;
    // use SoftDeletes;

    protected $table = 'eb_speaker';
    // protected $table = 'speaker';

    protected $hidden = [

        'email',
        // 'speaker_email',
    ];

    // 讲者关联护照
    public function passport()
    {
        return $this->hasOne(SpeakerPassport::class, 'speaker_id', 'id');
    }

    // 讲者关联简历
    public function resume()
    {
        return $this->hasOne(SpeakerResume::class, 'speaker_id', 'id');
    }

    /**
     * 类型转换
     *
     * @var array
     */
    // protected $casts = [
    //     'tags' => 'array',
    // ];


    public function getPhotoAttribute($value)
    {
        if (or_url($value)) {
            return $value;
        } else {
            return config('filesystems.disks.qiniu.domains.https') . $value;
        }
    }

    /**
     * 获取外籍讲者信息库列表V2
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-08-17
     * @param [int] $page
     * @param [int] $limit
     * @param [array] $where
     * @return mixed
     */
    public function getGuestSpeakerList($page, $limit, $where, $userId)
    {
        $where = function ($query) use ($where) {
            if (isset($where['global_search']) && $where['global_search'] != '') {
                $query->Where('name', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('title', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('country', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('city', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('country_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('city_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('country_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('city_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('time_zone', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('new_time_zone', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('jet_lag', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('institution', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('institution_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('institution_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('areas_of_expertise', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('areas_of_expertise_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('areas_of_expertise_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('medical_category', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('medical_category_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('medical_category_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('presenting_meeting', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('presenting_meeting_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('presenting_meeting_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('research_sponsor', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('association', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('association_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('association_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('journal_position', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('journal_position_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('journal_position_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('information_website', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('tags', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('remarks', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('remarks_en', 'like', '%' . $where['global_search'] . '%');;
            }
        };

        $list = $this->where('status', 1)
            ->where('is_display', 1)
            ->whereNull('delete_time')
            ->where($where)
            ->select(
                'id',
                'name',
                'title',
                'photo',
                // 'areas_of_expertise',
                'areas_of_expertise_cn',
                // 'areas_of_expertise_en',
                'country_cn',
                'country_en',
                'institution',
                'institution_cn',
                'institution_en',
                'remarks',
                'remarks_en',
                // 'note',
                'tags',
                'tags_en',
                'number_of_invitations'
            )
            ->orderBy('create_time', 'desc')
            ->paginate($limit);
        return $list;
    }


    /**
     * 获取外籍讲者信息库列表V2
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2022-08-17
     * @param [int] $page
     * @param [int] $limit
     * @param [array] $where
     * @return mixed
     */
    public function getGuestSpeakerLists($where)
    {
        $where = function ($query) use ($where) {
            if (isset($where['global_search']) && $where['global_search'] != '') {
                $query->Where('name', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('title', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('country', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('city', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('country_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('city_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('country_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('city_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('time_zone', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('new_time_zone', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('jet_lag', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('institution', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('institution_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('institution_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('areas_of_expertise', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('areas_of_expertise_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('areas_of_expertise_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('medical_category', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('medical_category_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('medical_category_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('presenting_meeting', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('presenting_meeting_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('presenting_meeting_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('research_sponsor', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('association', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('association_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('association_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('journal_position', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('journal_position_cn', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('journal_position_en', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('information_website', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('tags', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('remarks', 'like', '%' . $where['global_search'] . '%')
                    ->orWhere('remarks_en', 'like', '%' . $where['global_search'] . '%');;
            }
        };

        $list = $this->where('status', 1)
            ->where('is_display', 1)
            ->whereNull('delete_time')
            ->where($where)
            ->select(
                'id',
                'name',
                'title',
                'photo',
                'remarks',
                'remarks_en',
                'tags',
                'tags_en',
                'number_of_invitations'
            )
            ->orderBy('create_time', 'desc')
            ->get();
        return $list;
    }


    /**
     * 根据id、字段获取外籍讲者列表
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2023-09-06
     * @param int $page
     * @param int $limit
     * @param array $ids
     * @param string $filed
     * @return mixed
     */
    public function getForeignSpeakerList($page, $limit, $ids, $name, $filed = 'id')
    {
        $where = function ($query) use ($filed, $ids, $name) {
            $query->where('status', 1);
            $query->whereIn($filed, $ids);
            $query->whereNull('delete_time');
            if (isset($name) && $name != '') {
                $query->where('name', 'like', '%' . $name . '%');
            }
        };

        $list = $this->where($where)
            ->select(
                'id',
                'name',
                'title',
                'photo',
                // 'areas_of_expertise',
                'areas_of_expertise_cn',
                // 'areas_of_expertise_en',
                'country_cn',
                'country_en',
                'institution',
                'institution_cn',
                'institution_en',
                'remarks',
                'remarks_en',
                // 'note',
                'tags',
                'tags_en',
                'number_of_invitations'
            )
            ->orderBy('create_time', 'desc')
            ->paginate($limit);
        return $list;
    }
}
